package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.Brand;
import com.weixing.mall.provider.model.dto.BrandDto;
import com.weixing.mall.provider.model.query.BrandQuery;
import com.weixing.mall.provider.service.IBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName BrandController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/6
 * @Version 1.0
 **/
@RestController
@RequestMapping("brand")
@Api(value = "商品品牌APi", tags = "商品品牌")
public class BrandController extends BaseController {

    @Autowired
    private IBrandService brandService;

    @ApiOperation(value = "加载查询商品品牌列表")
    @GetMapping("")
    public Result list(BrandQuery brandQuery) {
        return ResultUtil.success(brandService.selectOfPage(initMpPage(), brandQuery));
    }

    @ApiOperation(value = "加载可用商品品牌列表")
    @GetMapping("/enable")
    public Result enableList(BrandQuery brandQuery) {
        return ResultUtil.success(brandService.list(new QueryWrapper<Brand>().eq("is_enable", WhetherEnum.YES.getValue())));
    }

    @ApiOperation(value = "保存商品品牌")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated BrandDto brandDto) {
      /*  try {
            List<Brand> brand = brandService.list(new QueryWrapper<Brand>().eq("zh_name", brandDto.getZhName()).or().eq("en_name", brandDto.getEnName()));

            if (brand.size()>0 && brandDto.getId() == null) {
                return ResultUtil.error("品牌的英文名称或者中文名称已经存在，请重新填写！");
            }
        } catch (Exception e) {
            return ResultUtil.error("出现未知错误，请重新联系管理员！");
        }*/

        brandService.save(brandDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除商品品牌")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "品牌id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result del(@PathVariable long id) {
        return brandService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }

    /**
     * @param ids
     * @return com.weixing.mall.base.wrapper.Result
     * @Description 只删除没有关联的品牌，见sql <br/>
     * @author gjq
     * @date 2019/8/9 14:18
     */
    @ApiOperation(value = "批量删除品牌")
    @PostMapping("delAll/{ids}")
    public Result delAll(@PathVariable int[] ids) {
        brandService.deleteAll(ids);
        return ResultUtil.success();
    }

}
